package com.etrans.web.gps.business.report.controller;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.report.domain.AreaAnalyseCarType;
import com.etrans.web.gps.business.report.domain.AreaAnalyzesVo;
import com.etrans.web.gps.business.report.domain.ProcessingStatistics;
import com.etrans.web.gps.business.report.service.EnterpriseProcessingStatisticsService;
import com.etrans.web.gps.business.report.service.EnterpriseRemindService;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.DateUtil;
import com.etrans.web.gps.util.grid.FlexiGridUtil;

@Controller
@RequestMapping("/base/report/enterpriseProcessingStatistics")
public class EnterpriseProcessingStatisticsController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(EnterpriseProcessingStatisticsController.class);

	@Autowired
	private EnterpriseProcessingStatisticsService enterpriseProcessingStatisticsService;
	
	/**
	 * 处理情况统计(企业)
	 * @return
	 */
	@RequestMapping("toEnterpriseProcessingStatistics.html")
	public String toEnterpriseProcessingStatistics(){
		return "views/business/report/enterpriseProcessingStatistics";
	}
	
	@RequestMapping(value = "queryProcessingStatistics.html")
	@ResponseBody
	public Map<String,Object> queryProcessingStatistics(String alarm_src, String vehicle_no, String alarm_type,
			String startTime, String endTime) {
		List<HashMap<String, Object>> personnelList = null;
		List<HashMap<String, Object>> typeList = null;
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			// 参数赋值
			HashMap<String, Object> paramsMap = new HashMap<String, Object>();
			paramsMap.put("alarm_src", alarm_src);
			paramsMap.put("vehicle_no", vehicle_no);
			paramsMap.put("alarm_type", alarm_type);
			paramsMap.put("startTime", startTime);
			paramsMap.put("endTime", endTime);
			//运维人员处警统计(柱形图)
			personnelList = enterpriseProcessingStatisticsService.queryProcessingPersonnelStatistics(paramsMap);
			//处理类型（圆形图）
			typeList = enterpriseProcessingStatisticsService.queryProcessingTypeStatistics(paramsMap);
			map.put("personnelList", personnelList);
			map.put("typeList", typeList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;

	}
	
	@RequestMapping(value = "queryLineListByHandleName.html")
	@ResponseBody
	public List<HashMap<String, Object>> queryLineListByHandleName(String alarm_src, String vehicle_no, String alarm_type,
			String startTime, String endTime, String handle_name) {
		List<HashMap<String, Object>> lineList = null;
		try {
			// 参数赋值
			HashMap<String, Object> paramsMap = new HashMap<String, Object>();
			paramsMap.put("alarm_src", alarm_src);
			paramsMap.put("vehicle_no", vehicle_no);
			paramsMap.put("alarm_type", alarm_type);
			paramsMap.put("startTime", startTime);
			paramsMap.put("endTime", endTime);
			paramsMap.put("handle_name", handle_name);
			//处警日期分布（折线图）
			lineList = enterpriseProcessingStatisticsService.queryLineListByHandleName(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lineList;
	}
	
	@RequestMapping(value = "queryTableListByHandleName.html")
	@ResponseBody
	public PageVo queryTableListByHandleName(String paramsGrid) {
		PageVo page = new PageVo();
		try {
			// 参数赋值
			HashMap<String, Object> paramMap = FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
			//列表
			List<ProcessingStatistics> rows = enterpriseProcessingStatisticsService.queryProcessingStatisticsListByHandleName(paramMap);
			page.setRows(rows);
			page.setPage((int) paramMap.get("page"));
			page.setTotal(enterpriseProcessingStatisticsService.queryProcessingStatisticsListCount(paramMap));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	@RequestMapping(value = "queryAllAlarmType.html")
	@ResponseBody
	public List<HashMap<String, Object>>  queryAllAlarmType() {
		List<HashMap<String, Object>>  list = new ArrayList<>();
		try {
			//列表
			list = enterpriseProcessingStatisticsService.queryAllAlarmType();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@RequestMapping(value = "exportEnterpriseProcessingStatistics.html")
	@ResponseBody
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void exportAreaAnalyzes( HttpServletRequest request, HttpServletResponse response, 
			String alarm_src, String vehicle_no, String alarm_type,String handle_name,
			String title, String StartPage, String EndPage, String PageSize,String colModel
			, String imgsrc,String startTime, String endTime) throws ParseException {
		//获取参数
		HashMap<String, Object> params = new HashMap<String, Object>();
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		List<String> imgsrcList   = JSON.parseObject(imgsrc, List.class);
		try {
			//分页条件
			int startPage = Integer.parseInt(StartPage);
			int endPage = Integer.parseInt(EndPage);
			int pageSize = Integer.parseInt(PageSize);
			params.put("fromNumber", startPage-1);
			params.put("pageSize", (endPage - startPage+1)*pageSize);	
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取参数失败",e);
		}	
		params.put("alarm_src", alarm_src);
		params.put("vehicle_no", vehicle_no);
		params.put("alarm_type", alarm_type);
		params.put("handle_name", handle_name);
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		List<ProcessingStatistics> list = enterpriseProcessingStatisticsService.queryProcessingStatisticsListByHandleName(params);
		for (ProcessingStatistics obj : list) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("vehicle_no", obj.getVehicle_no()==null ? "" : obj.getVehicle_no());
			map.put("alarm_name", obj.getAlarm_name()==null ? "" : obj.getAlarm_name());
			map.put("alarm_src", obj.getAlarm_src()==null ? "" : (obj.getAlarm_src()==1?"终端":"平台"));
			map.put("alarm_time", obj.getAlarm_time()==null ? "" : DateUtil.getDateTimeString(obj.getAlarm_time(), null));
			map.put("handle_type", obj.getHandle_type()==null ? "" :(obj.getHandle_type()==1?"系统处理":"人工处理"));
			map.put("handle_time", obj.getHandle_time()==null ? "" : DateUtil.getDateTimeString(obj.getHandle_time(), null));
			map.put("handle_remark", obj.getHandle_remark()==null ? "" : obj.getHandle_remark());
			rows.add(map);
		}
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		exportExl(request, response,title, removeHeadName(colModelList,-1,-1),rowsList,imgsrcList);
	}
	
}
